package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.mapper.QuestionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;


    @Override
    public void save(Exam exam) {
        //保存到exam表中,返回id
        examMapper.insert(exam);
        //获取单选的个数
        Integer oneCount = exam.getOneCount();
        //获取单选的id数组
        int[] oneArray = RandomUtil.oneArray(oneCount);
        for (int i = 0; i < oneArray.length; i++) {
            long queId = (long)oneArray[i];
            examMapper.saveRelation(exam.getId(),queId,i+1,exam.getOneScore());
        }

        //多选
        int[] manyArray = RandomUtil.manyArray(exam.getManyCount());
        for (int i = 0; i < manyArray.length; i++) {
            long queId = (long)manyArray[i];
            examMapper.saveRelation(exam.getId(),queId,i+1,exam.getManyScore());
        }

        //判断
        int[] judgeArray = RandomUtil.judgeArray(exam.getJudgeCount());
        for (int i = 0; i < judgeArray.length; i++) {
            long queId = (long)judgeArray[i];
            examMapper.saveRelation(exam.getId(),queId,i+1,exam.getJudgeScore());
        }
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    /*@Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }*/

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public Exam selectById(Long id) {
        return examMapper.selectById(id);
    }

    @Override
    public List<Long> selectQueIdByExamId(Long id) {
        return examMapper.selectQueIdByExamId(id);
    }
}
